<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/11/12
 * Time: 20:38
 */
class script_ali extends L {

    /**
     * 描述 : 抓取供应商信息
     * 作者 : Zoro.Zhu
     */
    public function getManu(){
        set_time_limit(0);
        $sql = "SELECT 
            productId
        FROM 
            `ali_product` 
        WHERE state = 50
        AND manuName = '' ";
        $info = L::sql($sql);
        foreach($info as $key => $row){
//            sleep(3);
            $manu = serv_rapi_aliReptile::getOfferId($row['productId']);
            if($manu === false){
                continue;
            }
            preg_match_all('/https:\/\/(\w+)\.1688./', $manu, $match);
            $manu = $match[1][0];
            echo $manu . " -> {$key}<br>";
            if(!empty($manu)){
                L::sql("UPDATE `ali_product` 
                SET `manuName` = '{$manu}' 
                WHERE
                    `productId` = '{$row['productId']}'");
            }else{
                echo '被屏了！';
                exit;
            }
        }
    }

    public function getManuStatus(){
        $sql = "SELECT * FROM `ali_manu` WHERE `manuName` = '80lp'";
        $info = L::sql($sql);
        foreach($info as $row){
            $manu = serv_rapi_aliReptile::getManu($row['manuName']);
            $status = serv_rapi_aliReptile::getManuStatus($manu);
            p($status);
        }
    }

    public function imgtest(){
        $imgSrc = "https://cbu01.alicdn.com/img/ibank/2014/209/632/1360236902_898943579.jpg";
        echo $imgSrc;
        $imgInfo = getimagesize($imgSrc);
        p($imgInfo);
        if($imgInfo[0] != $imgInfo[1] || $imgInfo[0] < 450 || $imgInfo[1] < 450 || $imgInfo['mime'] != 'image/jpeg'){
            echo 1212121;
        }
    }

    public function catInfo() {
        $catName = '女士家居服';
        $catName = mb_substr($catName, -3, 3, 'utf-8');
        if(strlen($catName) >= 9){
            echo $catName;
        }
    }

    /**
     * 描述 ：识别二维码图片
     * 作者 ：Zoro.Zhu
    **/
    public function imgCode(){
        //识别二维码图片
        $url = 'http://zxing.org/w/decode?u=http://zhuyh.utools.club/vs/data/image/1688/191126/598915580493/detail/001.jpg';
        $code = file_get_contents($url);
        preg_match("/<table id=\"result\">(.*)<\/table>/isU",$code,$math);
        preg_match("/<pre>(.*)<\/pre>/isU",$math[1],$maths);
        echo $maths[1];
    }

    /**
     * 描述 ：将非1688代销的产品下架
     * 作者 ：Zoro.Zhu
    **/
    public function marketOffer(){
        set_time_limit(0);
        $sql = "SELECT 
            productId,`data`,pddId,account 
        FROM 
            `ali_product` 
        WHERE state = 50";
        $info = L::sql($sql);
        echo time() . '<br>';
        foreach($info as $dd){
            $aliData = json_decode($dd['data'], true);
            $isConsignMarketOffer = 0;
            foreach ($aliData['bizGroupInfos'] as $row){
                //该产品是否为1688代销产品
                if($row['code'] == 'isConsignMarketOffer' && $row['support'] === true){
                    $isConsignMarketOffer = 1;
                    break;
                }
            }
            if($isConsignMarketOffer === 0){
                $token = model_vsAccount::getAccountToken(['account' => $dd['account']]);
                $data['accessToken'] = $token['accessToken'];
                $data['clientId'] = $token['appKey'];
                $data['clientSecret'] = $token['appSecret'];
                $data['isOnsale'] = 0;
                $data['goodsId'] = $dd['pddId'];
                $res = model_api_pdd::updateSaleStatus($data);
                if(!empty($res['error_msg']) && $res['error_msg'] == '商品不存在'){
                    L::sql("UPDATE `ali_product` 
                    SET `state` = '70' 
                    WHERE
                        `productId` = '{$dd['productId']}'");
                }else if(!empty($res['goods_sale_status_set_response'])){
                    L::sql("UPDATE `ali_product` 
                    SET `state` = '60' 
                    WHERE
                        `productId` = '{$dd['productId']}'");
                }
            }
        }
        echo time() . '<br>';
    }

    public function test1(){
        of_base_com_mq::set(['getAliLogistics', '765794019912614766'], ['orderId' => '765794019912614766']);
    }

}
return true;